package com.yanxinim.cn.ui.me.redpacket;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xuan.xuanhttplibrary.okhttp.HttpUtils;
import com.xuan.xuanhttplibrary.okhttp.callback.BaseCallback;
import com.xuan.xuanhttplibrary.okhttp.callback.ListCallback;
import com.xuan.xuanhttplibrary.okhttp.result.ArrayResult;
import com.xuan.xuanhttplibrary.okhttp.result.ObjectResult;
import com.yanxinim.cn.R;
import com.yanxinim.cn.adapter.BankCardAdapter;
import com.yanxinim.cn.adapter.BankNameAdapter;
import com.yanxinim.cn.bean.redpacket.BankInfoBean;
import com.yanxinim.cn.call.OnClickListener;
import com.yanxinim.cn.ui.base.BaseActivity;
import com.yanxinim.cn.util.ToastUtil;
import com.yanxinim.cn.view.PulldownPopupWindow;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import okhttp3.Call;

/**
 * @ClassName AddBankCardActivity
 * @Description 新增银行卡
 * @Author ChenXL
 * @Date 2021/5/25 19:42
 * @Version 1.0
 */
public class AddBankCardActivity extends BaseActivity {

    //银行选择框
    RelativeLayout rlSelectBank;
    //银行logo
    ImageView ivBankLogo;
    //银行名称
    TextView tvBankName;
    //持卡人姓名
    EditText etCardHolder;
    //持卡人卡号
    EditText etCardNumber;

    //选中的银行名称
    String selectBankName = "工商银行";
    //选中的银行编码
    String selectBankCode = "ICBC";

    List<BankInfoBean> bankInfoBeans = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_bank_card);
        initActionBar();
        initView();
        getBankList();
    }

    private void initActionBar() {
        getSupportActionBar().hide();
        findViewById(R.id.iv_title_left).setVisibility(View.GONE);
        TextView mTvLeft = (TextView) findViewById(R.id.tv_title_left);
        mTvLeft.setText("取消");
        mTvLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        TextView mTvTitle = (TextView) findViewById(R.id.tv_title_center);
        mTvTitle.setText("");
    }

    private void initView(){
        rlSelectBank = (RelativeLayout) findViewById(R.id.rlSelectBank);
        ivBankLogo = (ImageView) findViewById(R.id.ivBankLogo);
        tvBankName = (TextView) findViewById(R.id.tvBankName);
        etCardHolder = (EditText) findViewById(R.id.etCardName);
        etCardNumber = (EditText) findViewById(R.id.etCardNo);

        findViewById(R.id.tvSelectBank).setOnClickListener(view -> {
            showSelectBankAlart();
        });
        findViewById(R.id.ivArrowDown).setOnClickListener(view -> {
            showSelectBankAlart();
        });

        findViewById(R.id.btnAddCard).setOnClickListener(view -> {
            addCard();
        });
    }

    /**
     * 获取银行列表数据
     */
    private void getBankList(){
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("access_token", coreManager.getSelfStatus().accessToken);

        HttpUtils.get().url(coreManager.getConfig().BANK_LIST)
                .params(params)
                .build()
                .execute(new ListCallback<BankInfoBean>(BankInfoBean.class) {

                    @Override
                    public void onResponse(ArrayResult<BankInfoBean> result) {
                        if (result.getResultCode() == 1 && result.getData() != null) {
                            bankInfoBeans = result.getData();

                        } else {
                            ToastUtil.showErrorData(AddBankCardActivity.this);
                        }
                    }

                    @Override
                    public void onError(Call call, Exception e) {
                        ToastUtil.showNetError(AddBankCardActivity.this);
                    }
                });
    }

    /**
     * 显示选择银行弹框
     */
    private void showSelectBankAlart(){
        if (bankInfoBeans == null || bankInfoBeans.size() == 0){
            ToastUtil.showToast(AddBankCardActivity.this, "未获取到银行列表信息");
            return;
        }
        new PulldownPopupWindow(AddBankCardActivity.this, bankInfoBeans, rlSelectBank, true, onClickListener);
    }

    /**
     * 添加银行卡
     */
    private void addCard(){
        String cardName = etCardHolder.getText().toString().trim();
        String cardNo = etCardNumber.getText().toString().trim();

        if (TextUtils.isEmpty(cardName)){
            ToastUtil.showToast(AddBankCardActivity.this, "请输入持卡人姓名");
            return;
        }

        if (TextUtils.isEmpty(cardNo)){
            ToastUtil.showToast(AddBankCardActivity.this, "请输入持卡人银行卡号");
            return;
        }
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("access_token", coreManager.getSelfStatus().accessToken);
        params.put("bankCode", selectBankCode);
        params.put("bankName", selectBankName);
        params.put("cardName", cardName);
        params.put("bankCardNo", cardNo);

        HttpUtils.get().url(coreManager.getConfig().BANK_BIND)
                .params(params)
                .build()
                .execute(new BaseCallback<String>(String.class) {

                    @Override
                    public void onResponse(ObjectResult<String> result) {
                        if (result.getResultCode() == 1 ) {
                            ToastUtil.showToast(AddBankCardActivity.this, "绑卡成功");
                            setResult(RESULT_OK);
                            finish();
                        } else {
                            ToastUtil.showErrorData(AddBankCardActivity.this);
                        }
                    }

                    @Override
                    public void onError(Call call, Exception e) {
                        ToastUtil.showNetError(AddBankCardActivity.this);
                    }
                });
    }


    OnClickListener onClickListener = new OnClickListener() {
        @Override
        public void onClick(View view) {
            BankInfoBean bean = (BankInfoBean) view.getTag();
            selectBankCode = bean.getCode();
            selectBankName = bean.getName();
            ivBankLogo.setImageResource(BankNameAdapter.getBankLogo(selectBankCode));
            tvBankName.setText(selectBankName);
        }
    };
}
